package dhee.wtqshopproject.utils;

import dhee.wtqshopproject.dao.NewsMapper;
import dhee.wtqshopproject.entity.News;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class NewsMapperUtils {

    private static NewsMapperUtils newsMapperUtils;

    @Resource
    private NewsMapper newsMapper;

    @PostConstruct
    public void init() {
        newsMapperUtils = this;
        newsMapperUtils.newsMapper = this.newsMapper;
    }

    // 新增新闻（返回自增主键到传入的news对象中）
    public static void insertNews(News news) {
        newsMapperUtils.newsMapper.insertNews(news);
    }

    // 删除新闻
    public static void deleteNews(Integer newsId) {
        newsMapperUtils.newsMapper.deleteNews(newsId);
    }

    // 修改新闻
    public static void updateNews(News news) {
        newsMapperUtils.newsMapper.updateNews(news);
    }

    // 查询所有新闻（按发布时间倒序）
    public static List<News> selectAllNews() {
        return newsMapperUtils.newsMapper.selectAllNews();
    }

    // 根据ID查询单个新闻
    public static News selectNewsById(Integer newsId) {
        return newsMapperUtils.newsMapper.selectNewsById(newsId);
    }
}